package com.nanbei.a05;

import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: libs
 * @Created: 2024/11/14 14:22
 */
public class MapperPostProcessor implements BeanDefinitionRegistryPostProcessor {

    /*    @Bean
    public MapperFactoryBean<Mapper1> mapper1(SqlSessionFactory sqlSessionFactory){
        MapperFactoryBean<Mapper1> factory = new MapperFactoryBean<>(Mapper1.class);
        factory.setSqlSessionFactory(sqlSessionFactory);
        return factory;
    }
    */


    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanFactory) throws BeansException {
        try {
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            CachingMetadataReaderFactory readerFactory = new CachingMetadataReaderFactory();
            AnnotationBeanNameGenerator generator = new AnnotationBeanNameGenerator();
            // 扫描获取路径下的资源文件
            Resource[] resources = resolver.getResources("classpath*:com/nanbei/a05/mapper/**/*.class");
            for (Resource resource : resources) {
                MetadataReader metadataReader = readerFactory.getMetadataReader(resource);
                // 获取当前类路径下的源文件，用来判断是否是接口类型
                ClassMetadata classMetadata = metadataReader.getClassMetadata();
                if (classMetadata.isInterface()){

                    AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder
                            .genericBeanDefinition(MapperFactoryBean.class)   // 被spring 管理的对象是工厂类型 MapperFactoryBean
                            .addConstructorArgValue(classMetadata.getClassName()) // 设置 MapperFactoryBean 的构造参数 （类名）
                            .setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE) // 装配 sqlSessionFactory
                            .getBeanDefinition();

                    // 该定义用来设置 Mapper bean的名字
                    AbstractBeanDefinition db1 = BeanDefinitionBuilder
                            .genericBeanDefinition(classMetadata.getClassName())
                            .getBeanDefinition();
                    String beanName = generator.generateBeanName(db1, beanFactory);
                    beanFactory.registerBeanDefinition(beanName,beanDefinition);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
